﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

using Microsoft.Practices.Unity;

using Happy.ExtentionMethods;

namespace Happy.Bootstrap.Unity
{
    /// <summary>
    /// 扩展<see cref="IUnityContainer"/>。
    /// </summary>
    public static class IUnityContainerExtentions
    {
        /// <summary>
        /// 为<paramref name="serviceType"/>实现的所有接口类型注册服务。
        /// </summary>
        public static IUnityContainer RegisterTypeForAllInterface(this IUnityContainer that, Type serviceType, Func<LifetimeManager> lifecyleFunc)
        {
            that.MustNotNull("that");
            serviceType.MustNotNull("serviceType");
            lifecyleFunc.MustNotNull("lifecyleFunc");

            foreach (var interfaceType in serviceType.GetInterfaces())
            {
                that.RegisterType(interfaceType, serviceType, lifecyleFunc());
            }

            return that;
        }

        /// <summary>
        /// 为<paramref name="serviceType"/>实现的所有接口类型注册命名服务。
        /// </summary>
        public static IUnityContainer RegisterTypeForAllInterface(this IUnityContainer that, string name, Type serviceType, Func<LifetimeManager> lifecyleFunc)
        {
            that.MustNotNull("that");
            name.MustNotNull("name");
            serviceType.MustNotNull("serviceType");
            lifecyleFunc.MustNotNull("lifecyleFunc");

            foreach (var interfaceType in serviceType.GetInterfaces())
            {
                that.RegisterType(interfaceType, serviceType, name, lifecyleFunc());
            }

            return that;
        }
    }
}
